package cn.wolfcode.trip.app.util;

import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

//用户上下文工具类
public abstract class UserContext {
    public static final String USER_IN_SESSION = "USER_IN_SESSION";

    //获取session对象
    private static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //往session中设置当前登录用户
    public static void setCurrentUser(User user) {
        getSession().setAttribute(USER_IN_SESSION, user);
    }

    public static User getCurrentUser() {
        return (User) getSession().getAttribute(USER_IN_SESSION);
    }

}
